//—S—定义全局变量
//datagrid全局变量
var pageSize = 20;
var pageList = [ 20, 30, 40, 50 ];
var isClicked = false;
//—E—定义全局变量
//菜单树-表单增、删、查、改处理方法
$.fn.treeGridOptions = function(){}
//菜单树选中节点id返回
$.fn.treeGridOptions.doNode = function(){  
     var c="";  
     var p="";  
     $(".tree-checkbox1").parent().children('.tree-title').each(function(){  
         c+=$(this).parent().attr('node-id')+",";  
     });  
     $(".tree-checkbox2").parent().children('.tree-title').each(function(){  
           p+=$(this).parent().attr('node-id')+",";  
     });  
     var str=(c+p);  
     str=str.substring(0,str.length-1);  
     return(str);  
}
//菜单树节点选中
$.fn.treeGridOptions.nodeChk = function (treeObj,id){
    var node = treeObj.tree('find', id);
	if (node) {
	     var isLeaf = treeObj.tree('isLeaf', node.target);
		 if (isLeaf)  treeObj.tree('check', node.target);					 
	}
} 

//初始化左侧菜单
function InitLeftMenu(paramId) {
	var paramId = paramId || 0;
	$("#index_layout").layout('remove','west').layout('add',{    
	    region: 'west',    
	    width: 170,    
	    title: '' ,
	    content:'<div id="nav"></div>'
	}); 
	var $nav = $("#nav");
	var _menus = (function(){
		var result;
		$.ajax({
			type : 'post',
			url : "/modules/system/menu/getMenuList.html?parentId=" + paramId + "&randomTime=" + (new Date()).getTime(),
			dataType : 'json',
			async : false,
			success : function(data) {
				result = data.rows;
			}
		});
		return result;
	})();
	$nav.accordion({fit:false,border:false});
	var selectedPanelname = '';
	$.each(_menus, function(i, n) {
		$nav.accordion('add',{
		    title: n.name,
		    content: '<ul class="navlist" data-id="'+n.id+'"></ul>',
		    fit:false,
		    border:false,
		    iconCls: 'icon ' + n.iconCls
		});
		if(i != -1){
			selectedPanelname = n.name;
		}
	});
	$nav.accordion('select',selectedPanelname);
	
	$(".navlist").each(function(){
		var _this = $(this);
		$.ajax({
			type:'post',
			url:"/modules/system/menu/getMenuList.html?parentId=" + _this.attr("data-id") + "&randomTime=" + (new Date()).getTime(),
			dataType:'json',
			async:false,
			success:function(data){
				var menulist ='';
				$.each(data.rows,function(i,n){
					menulist += '<li><div><a ref="'+n.id+'" href="javascript:;" rel="' + n.href + '" ><span class="nav">' + n.name + '</span></a></div> </li>'	
				})
				_this.html(menulist);
			}
		});
	});
	
	$("#nav .panel-title").eq(0).trigger("click");
	
}



//打开具体菜单链接
$('.navlist li').live("click",function(ev){
	if(isClicked){return false;}
	isClicked = true;
	var it = $(ev.currentTarget ).find('a');
	var tabTitle = it.children('.nav').text();
	var url = it.attr("rel");
	var icon = it.find('.icon').attr('class');
	if(it.parent().siblings().html())
	{
		$('.third_ul').slideUp();
		var ul =it.parent().next();
		if(ul.is(":hidden"))
			ul.slideDown();
		else
			ul.slideUp();
	}
	else
	{
		addTab(tabTitle,url,icon);
		$('.navlist li div').removeClass("selected");
		it.parent().addClass("selected");
	}
	window.setTimeout('isClicked=false;',500);
	return false;
})




//数据编辑
$.fn.treeGridOptions.editFun = function (id,title,width,height,url){
	if (id == undefined) {
		var rows = treeGrid.treegrid('getSelections');
		id = rows[0].id;
	} else {
		treeGrid.treegrid('unselectAll').treegrid('uncheckAll');
	}
	if(url.indexOf("?")==-1)
	{
		url = url+'?id=' + id
	}
	else
	{
		url = url+'&id=' + id
	}
	parent.$.modalDialog({
		title : title,
		width : width,
		height : height,
		href : url,
		buttons : [ {
			text : '确定',
			handler : function() {
				parent.$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
				var f = parent.$.modalDialog.handler.find('#form');
				f.submit();
			}
		} ]
	});
}

//数据添加
$.fn.treeGridOptions.addFun = function(id,title,width,height,url) {
		if (id == undefined) {
			var rows = treeGrid.treegrid('getSelections');
			id = rows[0].id;
		} else {
			treeGrid.treegrid('unselectAll').treegrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : title,
			width : width,
			height : height,
			href : url+'?id='+id,
			buttons : [ {
				text : '添加',
				handler : function() {
					
					parent.$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
}
//单条数据删除
$.fn.treeGridOptions.deleteFun = function(id,url) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = treeGrid.treegrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			treeGrid.treegrid('unselectAll').treegrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要删除当前记录？', function(b) {
			if (b) {
				var currentUserId = '0';/*当前登录用户的ID*/
				if (currentUserId != id) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post(url, {
						id : id
					}, function(result) {
						if (result.result) {
							parent.$.messager.alert('提示', '操作成功', 'info');
							treeGrid.treegrid('reload');
						}
						parent.$.messager.progress('close');
					}, 'JSON');
				} else {
					parent.$.messager.show({
						title : '提示',
						msg : '不可以删除自己！'
					});
				}
			}
		});
	}

//数据表单提交
$.fn.treeGridOptions.formSubFun = function(formId,url,successMsg,failMsg){
     var successMsg = successMsg||"操作成功!";
     var failMsg = failMsg||"操作失败!";
     $(formId).form({
            url : url,
            onSubmit : function() {
                parent.$.messager.progress({
                    title : '提示',
                    text : '数据处理中，请稍后....'
                });
                var isValid = $(this).form('validate');
                if (!isValid) {
                    parent.$.messager.progress('close');
                }
                return isValid;
            },
            success : function(result) {
                parent.$.messager.progress('close');
                result = typeof result == 'object'?result: $.parseJSON(result);
                if (result.result) {
                    parent.$.modalDialog.openner_treeGrid.treegrid('reload');
                    parent.$.modalDialog.handler.dialog('close');
                    $.messager.alert('提示', successMsg, 'info');
                }else{
				    $.messager.alert('提示', result.msg, 'warning');
				}
            }
       });
}
/*结束*/

//表格-表单增、删、查、改处理方法
$.fn.dataGridOptions = function(){}
//数据编辑
$.fn.dataGridOptions.editFun = function (id,title,width,height,url,text){
		if(text==null || text == ''){
			text='确定';
		}
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		if (url.indexOf("?")==-1) {
			url = url+'?id=' + id;
		} else {
			url = url+'&id=' + id;
		}
		parent.$.modalDialog({
			title : title,
			width : width,
			height : height,
			href : url,
			buttons : [ {
				text : text,
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
}
//数据查看
$.fn.dataGridOptions.closeFun = function (id,title,width,height,url){
	if (id == undefined) {
		var rows = dataGrid.datagrid('getSelections');
		id = rows[0].id;
	} else {
		dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	}
	if (url.indexOf("?")==-1) {
		url = url+'?id=' + id;
	} else {
		url = url+'&id=' + id;
	}
	parent.$.modalDialog({
		title : title,
		width : width,
		height : height,
		href : url,
		buttons : [ {
			text : '关闭',
			handler : function() {
				parent.$.modalDialog.handler.dialog('close');
                parent.$.modalDialog.openner_dataGrid.datagrid('reload');
			}
		} ]
	});
}

//预览标
$.fn.dataGridOptions.viewLoanFun = function (id,title,width,height,url){
	if (url.indexOf("?")==-1) {
		url = url+'?id=' + id;
	} else {
		url = url+'&id=' + id;
	}
	parent.$.modalDialog({
			title : title,
			width : width,
			height : height,
			href : url,
			buttons : [{
				text : '关闭',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					parent.$.modalDialog.handler.dialog('close');
	                parent.$.modalDialog.openner_dataGrid.datagrid('reload');
				}
		} ]
	})
}

//数据添加
$.fn.dataGridOptions.addFun = function(id,title,width,height,url) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		if (url.indexOf("?")==-1) {
			url = url+'?id=' + id;
		} else {
			url = url+'&id=' + id;
		}
		parent.$.modalDialog({
			title : title,
			width : width,
			height : height,
			href : url,
			buttons : [ {
				text : '添加',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
}
//授权管理
$.fn.dataGridOptions.grantFun = function(id,title,width,height,url) {
		dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		parent.$.modalDialog({
			title : title,
			width : width,
			height : height,
			href : url +"?id="+ id,
			buttons : [ {
				text : '授权',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为授权成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}
//审核确认操作
$.fn.dataGridOptions.selectFun = function(id,title,width,height,url) {
    if (id == undefined) {
		  var rows = dataGrid.datagrid('getSelections');
		  id = rows[0].id;
	  }
	  parent.$.modalDialog({
			title : title,
			width : width,
			height : height,
			href : url+'?id='+id
		});
}
$.fn.dataGridOptions.confirmFun = function(id,title,width,height,url) {
      if (id == undefined) {
		  var rows = dataGrid.datagrid('getSelections');
		  id = rows[0].id;
	  }
	  parent.$.modalDialog({
			title : title,
			width : width,
			height : height,
			href : url+'?id='+id,
			buttons : [ {
				text : '确定',
				handler : function() {
					 parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					 f = parent.$.modalDialog.handler.find('#form');
					 isValid = f.form('validate');
					 if(isValid){
						 top.$.messager.confirm(title,"真的决定了吗?",function(r){
							if(r){
							   f.submit();
							}
						 });
					 }					
				}
			} ]
		});
}
//单条数据删除
$.fn.dataGridOptions.deleteFun = function(id,url,msg) {
        var dfMsg = "您是否要删除当前记录？";
		msg = msg || dfMsg;
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', msg, function(b) {
			if (b) {
				var currentUserId = '0';/*当前登录用户的ID*/
				if (currentUserId != id) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post(url, {
						id : id
					}, function(result) {
						parent.$.messager.progress('close');
						result =typeof result == 'object'?result: $.parseJSON(result);
						if (result.result) {
							parent.$.messager.alert('提示', '操作成功', 'info');
						}else{
							parent.$.messager.alert('提示', result.msg, 'info');
						}
						dataGrid.datagrid('reload');
					}, 'JSON');
				} else {
					parent.$.messager.show({
						title : '提示',
						msg : '不可以删除自己！'
					});
				}
			}
		});
}

//确认函数
$.fn.dataGridOptions.sureFun = function(id,url,msg,params) {
        var dfMsg = "您确认了吗？";
		msg = msg || dfMsg;
		if (typeof(params)!="object") params = eval("("+params+")");//转换字符串成对象
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', msg, function(b) {
			if (b) {
				if (id) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post(url, {
						id : id
					}, function(result) {
						parent.$.messager.progress('close');
					    if(result.success){
							parent.$.messager.alert('提示', result.msg, 'info');
						}else{						    
						    parent.$.messager.confirm('提示', result.msg, function(b){
							     if(b){
							        var type = result.reType;
							        $.fn.dataGridOptions.addFun(id,params.title[type],params.width,params.height,params.url[type]);
								  }
							})
						}
					    dataGrid.datagrid('reload');
					}, 'JSON');
				}
			}
		});
	}
//批量数据删除
$.fn.dataGridOptions.batchDeleteFun = function(url) {
		var rows = dataGrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.$.messager.confirm('确认', '您是否要删除当前选中的记录？', function(r) {
				if (r) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					var currentUserId = '0';/*当前登录用户的ID*/
					var flag = false;
					for ( var i = 0; i < rows.length; i++) {
						if (currentUserId != rows[i].id) {
							ids.push(rows[i].id);
						} else {
							flag = true;
						}
					}
					$.getJSON(url, {
						ids : ids.join(',')
					}, function(result) {
						if (result.success) {
							dataGrid.datagrid('load');
							dataGrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
						}
						if (flag) {
							parent.$.messager.show({
								title : '提示',
								msg : '不可以删除自己！'
							});
						} else {
							parent.$.messager.alert('提示', result.msg, 'info');
						}
						parent.$.messager.progress('close');
					});
				}
			});
		} else {
			parent.$.messager.show({
				title : '提示',
				msg : '请勾选要删除的记录！'
			});
		}
}
var searchType = 1;
//数据查询
$.fn.dataGridOptions.searchFun = function (formId) {
	if(formId=='#showAdw'){
		searchType = 2;
	}else{
		searchType = 1;
	}
	dataGrid.datagrid('load', $.serializeObject($(formId)));
}
//查询文件清除
$.fn.dataGridOptions.cleanFun = function(formId) {
	$(".type").each(function(){
		$(this).children().eq(0).attr("selected","selected");
	});
	$(".status").each(function(){
		$(this).children().eq(1).attr("selected","selected");
	});
	$(formId+' input').val('');
	dataGrid.datagrid('load', {});
	$("#showAdw")[0].reset();
}

//数据表单提交
$.fn.dataGridOptions.formSubFun = function(formId,url,successMsg,failMsg){
    var successMsg = successMsg||"操作成功!";
	var failMsg = failMsg||"操作失败!";
	$(formId).form({
	    url : url,
	    onSubmit : function() {
	        parent.$.messager.progress({
	            title : '提示',
	            text : '数据处理中，请稍后....'
	        });				
	        var isValid = $(this).form('validate');
	        if (!isValid) {
	            parent.$.messager.progress('close');
	        }
	        return isValid;
	    },
	    success : function(result) {
	        parent.$.messager.progress('close');
	        result =typeof result == 'object'?result: $.parseJSON(result);
	        if (result.result) {
	        	$.messager.alert('提示', successMsg, 'info');
	            parent.$.modalDialog.handler.dialog('close');
	            parent.$.modalDialog.openner_dataGrid.datagrid('reload');
	            parent.$.modalDialog.openner_dataGrid.datagrid('clearChecked');
	        }else{
			    $.messager.alert('提示', result.msg, 'warning');
	        }
	    }
	});
}

//批量发放
$.fn.dataGridOptions.formBatch = function(formId,url){

	$(formId).form({
	    url : url,
	    onSubmit : function() {
	    	
	        parent.$.messager.progress({
	            title : '提示',
	            text : '数据处理中，请稍后....'
	        });				
	        var isValid = $(this).form('validate');
	        if (!isValid) {
	            parent.$.messager.progress('close');
	        }
	        return isValid;
	        
	    },
    	success : function(result) {
	        parent.$.messager.progress('close');
	        result =typeof result == 'object'?result: $.parseJSON(result);
	       
        	$.messager.alert('提示', "批量发放"+ (result.sucNum+result.failNum) +"个：成功"+ result.sucNum+"个,失败：" + result.failNum +"个</br><a target='_blank' href='" + result.resultExcelPath + "'>下载处理结果</a>", 'warning');
            parent.$.modalDialog.openner_dataGrid.datagrid('reload');
            parent.$.modalDialog.openner_dataGrid.datagrid('clearChecked');
		    parent.$.modalDialog.handler.dialog('close');
	        
	    }
	});
}

//数据表单提交
$.fn.dataGridOptions.viewformSubFun = function(formId,url,successMsg,failMsg){
    var successMsg = successMsg||"操作成功!";
	var failMsg = failMsg||"操作失败!";
	$(formId).form({
	    url : url,
	    onSubmit : function() {
	    	
	        if ($("#uploadPiclist3") && $("#uploadPiclist3").children().length == 1) {
	        	$.messager.alert('提示', "至少上传一张公司照", 'warning');
	        	return false;
	        }
	        if($('#isDS') && $('#isDS').attr("checked") && $("#fixedTime").val() == '')
	        {
	        	$.messager.alert('提示','请输入定时时间');
	        	return false;
	        }
	        if($("#openExperience").attr("checked")){
	        	
	        }else{
	        		
	        }
	        parent.$.messager.progress({
	            title : '提示',
	            text : '数据处理中，请稍后....'
	        });				
	        var isValid = $(this).form('validate');
	        if (!isValid) {
	            parent.$.messager.progress('close');
	        }
	        return isValid;
	    },
	    success : function(result) {
	        parent.$.messager.progress('close');
	        result =typeof result == 'object'?result: $.parseJSON(result);
	        if (result.result) {
	        	$.messager.alert('提示', successMsg, 'info');
	            parent.$.modalDialog.handler.dialog('close');
	            parent.$.modalDialog.openner_dataGrid.datagrid('reload');
	            parent.$.modalDialog.openner_dataGrid.datagrid('clearChecked');
	        }else{
			   // $.messager.alert('提示', result.msg, 'warning');
	        	$("#contractmsg").html(result.msg);
	        }
	    }
	});
}

/*结束*/
/*表格处理*/
//数据表单提交
$.fn.formOptions = function(){};
$.fn.formOptions.viewFun =  function(id,title,width,height,url){
	if (id == undefined) {//点击右键菜单才会触发这个
		var rows = dataGrid.datagrid('getSelections');
		id = rows[0].id;
	}
	if (url.indexOf("?")==-1) {
		url = url+'?id=' + id;
	} else {
		url = url+'&id=' + id;
	}
	parent.$.modalDialog({
		title : title,
		width : width,
		height : height,
		href : url
	});
}
//清理
$.fn.formOptions.btnSubFun = function(formId,url,successMsg,failMsg){
	var successMsg = successMsg||"操作成功!";
	var failMsg = failMsg||"操作失败!";	 
	$(formId).form('submit',{
	    url : url,
	    onSubmit : function() {
	        parent.$.messager.progress({
	            title : '提示',
	            text : '数据处理中，请稍后....'
	        });				
	        var isValid = $(this).form('validate');
	        if (!isValid) {
	            parent.$.messager.progress('close');
	        }
	        return isValid;
	    },
	    success : function(result) {
	        parent.$.messager.progress('close');
	        result = typeof result == 'object'?result: $.parseJSON(result);
	        if (result) {
				top.$.messager.alert('提示', successMsg, 'info');
	        }else{
			    top.$.messager.alert('提示', failMsg, 'warning');
			}
	    }
	});
}

//options控制区块显示、隐藏
$.fn.formOptions.optionsCtl = function(obj){
    var childLab = obj.parent().next(".child-options");
	var type = obj.val();
	    if(type==1){
	      childLab.show();
		  childLab.find("input").addClass("easyui-validatebox validatebox-text");
		}else{
	      childLab.hide();
		  childLab.find("input").removeClass("easyui-validatebox validatebox-text validatebox-invalid");
		}
}
//平滑数据格式转换
$.fn.treeConvert = function(){}
//菜单平滑数据格式转换
$.fn.treeConvert.menu = function(rows){
		function exists(rows, parentId){
			for(var i=0; i<rows.length; i++){
				if (rows[i].id == parentId) return true;
			}
			return false;
		}		
		var nodes = [];
		// 获取顶级菜单
		for(var i=0; i<rows.length; i++){
			var row = rows[i];
			if (!exists(rows, row.parentId)){
				nodes.push({id:row.id,text:row.name,iconCls:row.iconCls,attributes:{href:row.href}});
			}
		}
		var toDo = [];
		for(var i=0; i<nodes.length; i++){
			toDo.push(nodes[i]);
		}
		while(toDo.length){
			var node = toDo.shift();	// 父级节点
			// 获取子级节点
			for(var i=0; i<rows.length; i++){
				var row = rows[i];
				if (row.parentId == node.id){
					var child = {id:row.id,text:row.name,iconCls:row.iconCls,attributes:{href:row.href}};					
					if (node.children){
						node.children.push(child);
					} else {
						node.children = [child];
					}					
					toDo.push(child);
				}
			}
		}
		return nodes;
}
//iframe 页面最小宽度设置
$.fn.iframeWidthResize = function(width){
    $("body",parent.document).find('iframe').each(function(i){
	    var thisUrl = window.location.href;
		var frameUrl = $(this).attr('src');
	    if(thisUrl.indexOf(frameUrl)>0||thisUrl == frameUrl){
		   $(this).css({"min-width":width});
		}
	});
}
//文件下载
$.fn.downloadFun = function(id,url){
   if (id == undefined) {
	   var rows = dataGrid.datagrid('getSelections');
	   id = rows[0].id;
   } 

   url = url + "?id=" + id;
   $.ajax({
	  "url": url + "&randomTime=" + (new Date()).getTime(),
	  "type": "GET",
	  "success": function(data){
		  window.location.href = url;
	  },
	  "error": function(){
		  $.messager.alert("消息提醒：","文件下载失败，请稍候再试！","warning");		
	 }
  });
}
//数据类型转字符串
var typeToStr = function(data,type,status){
   var result = '';
   $.each(data,function(index,item){
	  if(type == parseInt(item.value)&&item.type== status)		   result =  item.name;
   });
   return result;
}
//浏览小图标
var showIcons = function() {
	var dialog =  parent.sy.modalDialog({
			title : '浏览小图标',
			width:520,
			height:398,
			url : '/icons.html',
			buttons : [{
				text : '确定',
				handler : function() {
					dialog.find('iframe').get(0).contentWindow.selectIcon(dialog, $('#iconCls'));
				}
			}]
	});
};

//添加tab页   
function addTab(subtitle,url,icon){
	if(!$('#index_tabs').tabs('exists',subtitle)){
		$('#index_tabs').tabs('add',{
			title:subtitle,
			content:createFrame(url),
			closable:true
			//closable:true,
			//icon:icon
		});
	}else{
		$('#index_tabs').tabs('select',subtitle);
		$('#index_tabs').tabs('getSelected').panel('refresh');
	}
}

function addCollateralPage(id,title,url){
	if(!parent.$('#index_tabs').tabs('exists',title)){
		var url = url+'?id=' + id;
		parent.$('#index_tabs').tabs('add',{
			title:title,
			content:createFrame(url),
			closable:true
		});
	}else{
		parent.$('#index_tabs').tabs('select',title);
		var url = url+'?id=' + id;
		var tab = parent.$('#index_tabs').tabs('getSelected'); 
		parent.$('#index_tabs').tabs('update', {
			tab: tab,
			options: {
				title:title,
				content:createFrame(url),
				closable:true
			}
		});
		tab.panel('refresh');
	}
	
}

function createFrame(url){
	var s = '<iframe allowtransparency="true" scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
	return s;
}

//调用方法 addGoodsTab（{title：参数值,url：参数值,iconCls:参数值}）
var addGoodsTab = function(params) {
   	var iframe = '<iframe src="' + params.url + '" frameborder="0" style="border:0;width:100%;height:100%;"></iframe>';
	var t = parent.$('#index_tabs');
	var opts = {
		title : params.title,
		closable : true,
		content : iframe,
		border : false,
		fit : true
	};
	if (t.tabs('exists', opts.title)) {
		t.tabs('select', opts.title);
		parent.$.messager.progress('close');
	} else {
		t.tabs('add', opts);
	}
}
//从grid列表页添加tab页   
var gridAddTab = function(params,id,borrowNo) {
    if(typeof(params)!="object") params = eval("("+params+")");
   	var iframe = '<iframe  allowtransparency="true" src="' + params.url + id+'" frameborder="0" style="border:0;width:100%;height:98%;"></iframe>';
	var t = parent.$('#index_tabs');
	var opts = {
		title : params.title.replace("{$borrowNo}",borrowNo),
		closable : true,
		iconCls : params.iconCls,
		content : iframe,
		border : false,
		fit : true
	};
	if (t.tabs('exists', opts.title)) {
		t.tabs('select', opts.title);
		parent.$.messager.progress('close');
	} else {
		t.tabs('add', opts);
	}
}
//时间戳转换
var getLocalTime = function(value,type) {
    if (value == null || value == '') {
        return '';
    }
	var dt;
	if (value instanceof Date) {
	    dt = value;
	}
	else {
	    dt = new Date(value);
	    if (isNaN(dt)) {
	        value = value.replace(/\/Date\((-?\d+)\)\//, '$1'); //将那个长字符串的日期值转换成正常的JS日期格式
	        dt = new Date();
	        dt.setTime(value);
	    }
	}
	 switch (type){
	 case 1:
		 return dt.format("yyyy年MM月dd日");   //这里用到一个javascript的Date类型的拓展方法
		 break;
	 case 2:
		 return dt.format("yyyy年MM月dd日 hh:mm:ss");   //这里用到一个javascript的Date类型的拓展方法
		 break;
	 case 3:
		 return dt.format("yyyy-MM-dd");   //这里用到一个javascript的Date类型的拓展方法
		 break;
	 case 4:
		 return dt.format("yyyy-MM-dd hh:mm:ss");   //这里用到一个javascript的Date类型的拓展方法
		 break;
	 }

}

// 日期显示的格式化	
Date.prototype.format = function (format) 
{
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(),    //day 
        "h+": this.getHours(),   //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter 
        "S": this.getMilliseconds() //millisecond 
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

/*
 * MAP对象，实现MAP功能
 *
 * 接口：
 * size()     获取MAP元素个数
 * isEmpty()    判断MAP是否为空
 * clear()     删除MAP所有元素
 * put(key, value)   向MAP中增加元素（key, value) 
 * remove(key)    删除指定KEY的元素，成功返回True，失败返回False
 * get(key)    获取指定KEY的元素值VALUE，失败返回NULL
 * element(index)   获取指定索引的元素（使用element.key，element.value获取KEY和VALUE），失败返回NULL
 * containsKey(key)  判断MAP中是否含有指定KEY的元素
 * containsValue(value) 判断MAP中是否含有指定VALUE的元素
 * values()    获取MAP中所有VALUE的数组（ARRAY）
 * keys()     获取MAP中所有KEY的数组（ARRAY）
 *
 * 例子：
 * var map = new Map();
 *
 * map.put("key", "value");
 * var val = map.get("key")
 * ……
 *
 */
function Map() {
    this.elements = new Array();

    //获取MAP元素个数
    this.size = function() {
        return this.elements.length;
    };

    //判断MAP是否为空
    this.isEmpty = function() {
        return (this.elements.length < 1);
    };

    //删除MAP所有元素
    this.clear = function() {
        this.elements = new Array();
    };

    //向MAP中增加元素（key, value) 
    this.put = function(_key, _value) {
        this.elements.push( {
            key : _key,
            value : _value
        });
    };

    //删除指定KEY的元素，成功返回True，失败返回False
    this.remove = function(_key) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key) {
                    this.elements.splice(i, 1);
                    return true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    };

    //获取指定KEY的元素值VALUE，失败返回NULL
    this.get = function(_key) {
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key) {
                    return this.elements[i].value;
                }
            }
        } catch (e) {
            return null;
        }
    };

    //获取指定索引的元素（使用element.key，element.value获取KEY和VALUE），失败返回NULL
    this.element = function(_index) {
        if (_index < 0 || _index >= this.elements.length) {
            return null;
        }
        return this.elements[_index];
    };

    //判断MAP中是否含有指定KEY的元素
    this.containsKey = function(_key) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key) {
                    bln = true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    };

    //判断MAP中是否含有指定VALUE的元素
    this.containsValue = function(_value) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].value == _value) {
                    bln = true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    };

    //获取MAP中所有VALUE的数组（ARRAY）
    this.values = function() {
        var arr = new Array();
        for (i = 0; i < this.elements.length; i++) {
            arr.push(this.elements[i].value);
        }
        return arr;
    };

    //获取MAP中所有KEY的数组（ARRAY）
    this.keys = function() {
        var arr = new Array();
        for (i = 0; i < this.elements.length; i++) {
            arr.push(this.elements[i].key);
        }
        return arr;
    };
}

//添加操作员-用户名校验-添加
$("#verifyOperUserName").live("blur",function(){
	var verifyOperUserNameVal = $("#verifyOperUserName").val();
	if($(this).val()!=''){
		$.ajax({
			type:'post',
			url:'/modules/system/operator/checkOperUserName.html?randomTime=' + (new Date()).getTime(),
			async:false,
			data:{userName:$(this).val()},
			dataType:"json",
			success:function(data){
				if(!data.result){
					top.$.messager.alert('提示','该用户名已存在','info');
					$("#verifyOperUserName").val("");
				}
			}
		});
	}
})

//添加操作员-用户名校验-编辑
$("#verifyOperUserNameEdite").live("blur",function(){
	var verifyOperUserNameVal = $("#verifyOperHideUserName").val();
	if($(this).val()!='' && $(this).val() != verifyOperUserNameVal){
		$.ajax({
			type:'post',
			url:'/modules/system/operator/checkOperUserName.html?randomTime=' + (new Date()).getTime(),
			async:false,
			data:{userName:$(this).val()},
			dataType:"json",
			success:function(data){
				if(!data.result){
					top.$.messager.alert('提示','该用户名已存在','info');
					$("#verifyOperUserNameEdite").val(verifyOperUserNameVal);
				}
			}
		});
	}
})

//划出账户添加
var selectTakeoutAcc = function(merchantNo,takeAccType) {
	dialog =  parent.sy.modalDialog({
		title : '选择账户',
		width:520,
		height:450,
		url : '/modules/accountTransfer/getAccountListPage.html?merchantNo='+merchantNo+"&takeAccType="+takeAccType,
		buttons : [{
			text : '确定',
			handler : function() {
				dialog.find('iframe').get(0).contentWindow.getSelected(dialog, $('#takeOutAccNo'));
			}
		}]
	});
};

//划入账户添加
var selectTakeAcc = function(merchantNo,takeAccType) {
	dialog =  parent.sy.modalDialog({
		title : '选择账户',
		width:520,
		height:450,
		url : '/modules/accountTransfer/getAccountListPage.html?merchantNo='+merchantNo+"&takeAccType="+takeAccType,
		buttons : [{
			text : '确定',
			handler : function() {
				dialog.find('iframe').get(0).contentWindow.getSelected(dialog, $('#takeInAccNo'));
			}
		}]
	});
};


function exportExcel(that){	
	var url = $(that).data("url");
	var exportTitle = $(that).data("title");
	var params = (url.indexOf('?')!=-1?'&':'?') + 'exportTitle=' + exportTitle + getFieldsAndHearders();
	var keywords =  $("#keywords").val();
	if(searchType == 1){
		params += '&keywords='+encodeURI(keywords);
	}else{
		params += '&'+$('#showAdw').serialize();
	}
	var hrefval = url + params;
	window.location.href=hrefval;
}

function getFieldsAndHearders(){
	var hearders = '', fields = '';
	var opts = $('#dataGrid').datagrid('getColumnFields');
	for(var i= 0;i<opts.length;i++){
		var col = $('#dataGrid').datagrid('getColumnOption',opts[i]); 
		
		if(col.field=='action' || col.hidden){
			continue;
		}else{
			hearders += col.title+",";
			fields  += col.field+",";
		}		
	}
	hearders = hearders.substring(0,hearders.length-1);
	fields = fields.substring(0,fields.length-1);
	return '&hearders='+hearders+"&fields="+fields;
}

